#include <HTTPClient.h>
#include<ArduinoJson.h>
// 心知天气api-key和城市
extern String key;
extern String city;

extern XFont *_xFont; 
extern TFT_eSPI tft;   
extern TFT_eSprite spr;


extern void getWeather(){
  String URL = "https://api.seniverse.com/v3/weather/now.json?key=" + key + "&location=" + city + "&language=zh-Hans&unit=c";

  HTTPClient httpClient;
  httpClient.begin(URL);

  httpClient.setUserAgent("Mozilla/5.0 (iPhone; CPU iPhone OS 11_0 like Mac OS X) AppleWebKit/604.1.38 (KHTML, like Gecko) Version/11.0 Mobile/15A372 Safari/604.1");
  // 不加这一句拿不到对应编号 感觉这里是特意处理了 不建议一直调用
  httpClient.addHeader("Referer", "https://api.seniverse.com");
  Serial.println("正在获取天气数据...");
  int httpCode = httpClient.GET();
  if(httpCode != 200){
      Serial.println("获取天气数据失败!");
      httpClient.end();
      return;
  }
  String resp = httpClient.getString();
  //Serial.println(resp);
  DynamicJsonDocument doc(1024);

  deserializeJson(doc, resp);

  const char* weather = doc["results"][0]["now"]["text"];
  const char* temper = doc["results"][0]["now"]["temperature"];
  // 还需要一个清除画布的过程
  spr.setColorDepth(8);
  spr.createSprite(40, 50);
  spr.fillSprite(TFT_WHITE);
  spr.pushSprite(180, 30);
  spr.deleteSprite();

  tft.setCursor(180, 20, 4);
  tft.printf("%s",temper);

  _xFont->DrawChinese(210, 25, "℃", TFT_BLACK);
  _xFont->DrawChinese(180, 50, weather, TFT_BLACK);

  //Serial.printf("%s℃ %s", weather, temper);


}

